
import 'dart:io';
import 'package:flutter/services.dart';


enum ResultType {
  done,
  fileNotFound,
  noAppToOpen,
  error,
}

class FileOpen {
  static FileOpen _instance = FileOpen._();
  factory FileOpen() => _getInstance();
  static FileOpen get instance => _getInstance();
  FileOpen._();
  static FileOpen _getInstance() {
    return _instance;
  }
  static const MethodChannel _channel =
      const MethodChannel('xyx/file_open');
  
  static ResultType intToResultType(int code) {
    switch (code) {
      case -1:
        return ResultType.noAppToOpen;
      case -2:
        return ResultType.error;
      case -3:
        return ResultType.fileNotFound;
    }
    return ResultType.done;
  }
  
  Future<ResultType> openFile(String path)async{
    File file = File(path);
    if(file.existsSync()){
      int result = await _channel.invokeMethod('open_file',{'path':path});
      return intToResultType(result);
    }else{
      return ResultType.fileNotFound;
    }
  }
}
